package com.easyctrl.fragment;

import java.util.ArrayList;

import android.os.Bundle; 
import android.view.KeyEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListAdapter;

import com.easyctrl.adapter.VirtualMLIstAdapter;
import com.easyctrl.iface.OnBackDown;
import com.easyctrl.iface.OnBaseFragmentList;
import com.easyctrl.ldy.activity.MainApplication;
import com.easyctrl.ldy.activity.R;
import com.easyctrl.ldy.domain.ModulePortBean;
import com.easyctrl.ldy.domain.TextVirtural;
import com.easyctrl.ldy.domain.TimerBean; 
import com.easyctrl.ldy.util.SaveSingleObject;
import com.easyctrl.ui.base.BaseActivity;

public class VirtualMList extends BaseFragmentListview implements OnBaseFragmentList,OnBackDown{
 
	private String Tag = VirtualMList.class.getSimpleName();
	
	private ArrayList<String> beans;
	private VirtualMLIstAdapter adapter;
	
	public static final int VIRTUALTYPE = 1;
	public static final int USERTYPE = 2;
	
	private int fragmentType;  // 1 2
	   
	@Override
	public void onCreate(Bundle savedInstanceState) { 
		super.onCreate(savedInstanceState);
		this.setOnBaseFragmentList(this); 
		activity.setOnBackDown(this); 
		MainApplication.virtualManager.batchSave();
		beans = MainApplication.virtualManager.findArrayType();
		adapter = VirtualMLIstAdapter.getAdapter(getActivity(), beans);
	} 
	public void setFragmentType(int fragmentType) {
		this.fragmentType = fragmentType;
	}

	@Override
	public ListAdapter onAdapter() { 
		return adapter;
	}

	@Override
	public void onItemListviewClick(AdapterView<?> parent, View view,int position, long id) { 
		
		TextVirtural textVirtural = new TextVirtural();
		textVirtural.beans = MainApplication.modulePortAdapterSimple.getSelects();
		String type = beans.get(position);
		VirtualKeyGridView gridView = new VirtualKeyGridView();
		int startkey = Integer.valueOf(type);
		gridView.setType(startkey);
		textVirtural.type = startkey;
		gridView.setSelectBeans(MainApplication.modulePortAdapterSimple.getSelects());
		textVirtural.beans = selects;
		int start = startkey * 10 + 1;
		int end = start + 9;
		String result = String.valueOf(start) +" ~ "+ String.valueOf(end);
		gridView.setModel(result);
		textVirtural.model = result;
		gridView.setObject(object);
		textVirtural.object = object;
		if(selectTimer != null && selectTimer.size()>0){
			gridView.setSelectTimerBean(selectTimer.get(0));
		} 
		textVirtural.selectTimer = selectTimer;
		SaveSingleObject.getInstance().setTextObject(textVirtural);
		activity.pushFragments("setting", gridView, true, false, R.id.setting_center);
	}
	
	@Override
	public void onClick(View v) { 
		Object obj = SaveSingleObject.getInstance().getTextObject();
		
		if(obj instanceof TextVirtural){
			
			TextVirtural textVirtural = (TextVirtural)obj;
			
			if(textVirtural != null){
				textVirtural.isListBack = true;
			} 
		}
		activity.popFragments(BaseActivity.SETTING,R.id.setting_center,true);
	}

	private ArrayList<ModulePortBean> selects;
	public void setSelectBeans(ArrayList<ModulePortBean> selects) { 
		this.selects = selects;
	}

	private ArrayList<TimerBean> selectTimer;
	
	public void setSelectTimer(ArrayList<TimerBean> selects) { 
		this.selectTimer = selects;
	}

	private boolean isBack = false;
	
	@Override
	public void onResume() { 
		super.onResume();
		isBack = true;
		MainApplication.userManager.setCurrentFragment(Tag);
	}
	
	@Override
	public void onPause() { 
		super.onPause();
		isBack = false;
	}
	
	private Object object; 
	public void setObject(Object object) {
		this.object = object;
	}

	@Override
	public void onKeyDonw(int keyCode, KeyEvent event) { 
		if(keyCode == KeyEvent.KEYCODE_BACK){ 
			if(isBack){
				activity.popFragments(BaseActivity.SETTING,R.id.setting_center,true);
//				if(fragmentType == USERTYPE){
//					MainApplication.userManager.setCurrentFragment("UserSetFragment"); 
//				}else {
//					MainApplication.userManager.setCurrentFragment("VirtualFragmentList"); 
//				}
			}
		} 
	}
}
